import { getBaseUrl } from '@/assets/utils';
import { post, get } from "../api/axios";
import instance from "../api/axios";

/**
 * 通过code获取用户信息
 */
export const getUserInfoByCode = (params, code) =>
    new Promise((resolve, reject) => {
        const url = `${getBaseUrl().article}/api/admin/1/wx/wechat/author/userinfo?code=${code}`;
        post(
            url, params
        ).then(res => {
            const { data } = res.data;
            resolve(data);
        }).catch(() => {
            reject('程序开小差了，请稍后再试');
        });
    });

//通过bid判断业务类型bizType
export const getBidType = (bid) =>
    new Promise((resolve, reject) => {
        get(`${getBaseUrl().bizType}/api/1/qq/group/bizType?bid=${bid}`)
            .then(res => {
                console.log(res);
                const { data } = res;
                resolve(data);
            }).catch(() => {
                reject('程序开小差了，请稍后再试');
            });
    })

// 进入h5落地页
export const enter_loadH5 = (params, headers) =>
    new Promise((resolve, reject) => {
        const url = `${getBaseUrl().jj}/h5/openApi/v1.0/market-extension/clue-clientinfo/add`;
        instance({
            url,
            method: 'POST',
            data: params,
            headers: headers,
            transformRequest: data => JSON.stringify(data)
        })
            .then(res => {
                const { data } = res;
                resolve(data);
            })
            .catch(() => {
                reject('程序开小差了，请稍后再试');
            });
    })

// 通过channelId获取JSTicket
export const getJsTicket = params =>
    new Promise((resolve, reject) => {
        get(`${getBaseUrl().article}/api/admin/1/${params.channelId
            }/js/ticket/get`, params)
            .then(res => {
                const { data } = res;
                resolve(data);
            })
            .catch(() => {
                reject('程序开小差了，请稍后再试');
            });
    })

//微信jsApi支付
export const wxJsPay = params =>
    new Promise((resolve, reject) => {
        const url = `${getBaseUrl().jj}/koApi/v1.0/pay-trade/spread/js/pay/v4`;
        post(
            url, params, {
            headers: {
                'Content-Type': 'application/json'
            }
        }
        ).then(res => {
            const { data } = res;
            resolve(data);
        }).catch(() => {
            reject('程序开小差了，请稍后再试');
        })
    })

// 获取投顾信息
export const getWechatByBid = params =>
    new Promise((resolve, reject) => {
        get(`${getBaseUrl().yskj}/api/2/h5/qq/dispatch.json?bid=${params}`,
            params
        ).then(res => {
            const { data } = res;
            resolve(data);
        }).catch(() => {
            reject('程序开小差了，请稍后再试');
        })
    })

// 同步生成一客一码
export const qrcodeWithParams = (params, bizType,headers) =>
    new Promise((resolve, reject) => {
        const url = `${getBaseUrl(bizType)
            }/h5/openApi/v1.0/webox/corp/internal/build/contactCode/revise`;

        instance({
            url,
            method: 'POST',
            data: params,
            headers: headers,
            transformRequest: data => JSON.stringify(data)
        })
            .then(res => {
                const { data } = res;
                resolve(data);
            })
            .catch(() => {
                reject('程序开小差了，请稍后再试');
            });
    });

    //  一客一码锁定
    export const getCodeLock = (params, bizType,headers) =>
    new Promise((resolve, reject) => {
        get(`${
            getBaseUrl(bizType)
            }/h5/openApi/v1.0/webox/corp/contactMe/getQrCode`,
              params,
              headers
          ) .then(res => {
            const { data } = res;
            resolve(data);
        })
        .catch(() => {
            reject('程序开小差了，请稍后再试');
        });
    })